Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'债项号',dataIndex:'debtNo',hidden:true},
{header:'债项编号',dataIndex:'debtId',width:100},
{header:'债项名称',dataIndex:'debtDesc',width:100},
{header:'合同金额(元)',dataIndex:'debtSum',width:100},
{header:'信贷余额(元)',dataIndex:'debtBalance',width:100},
{header:'债项币种',dataIndex:'currency',width:100},
{header:'债项业务品种',dataIndex:'debtType',width:100},
{header:'初分结果',dataIndex:'firstResult',width:100},
{header:'初分人',dataIndex:'fruserId',width:100},
{header:'初分机构',dataIndex:'frorgId',width:100},
{header:'分类认定流水号',dataIndex:'serialNo',hidden:true}
]); 

var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'obligation/listClassifyRecordToConfirm.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'debtNo', mapping:'debt.debtNo'},
{name:'debtId',mapping:'debt.debtId'}, 
{name:'debtDesc', mapping:'debt.debtDesc'},
{name:'debtSum', mapping:'debt.debtSum'},
{name:'debtBalance', mapping:'debt.debtBalance'},
{name:'currency', mapping:'debt.currency'},
{name:'debtType', mapping:'debt.debtType'},
{name:'firstResult', mapping:'classify.firstResult'},
{name:'fruserId', mapping:'classify.fruserId'},
{name:'frorgId', mapping:'classify.frorgId'},
{name:'serialNo', mapping:'classify.serialNo'}
]) 
}); 
ds.load({params:{start:0,limit:20}}); 

var grid=new Ext.grid.GridPanel({ 
id:'contentList',
renderTo:"list", 
stripeRows:true,//斑马线效果
title:"资产分类认定-待认定列表", 
height:500, 
cm:cm,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'调整初分结果',id: 'btnAdd'},
 '-',
 {text:'认定结果提交',handler:classifyConfirm},
 '-',
 {text:'退回初分人',handler:backToFirst}
 ]              
});

var classifyResultStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=ClassifyResult',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
classifyResultStore.load();

//新增表单
addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
width: 480, 
height: 500, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 100, 
items: 
[ { xtype: 'fieldset', 
title: '调整分类信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ { layout : "form",
    items : [{
    	xtype: 'combo', 
    	 id : "firstResult",
    	              store: classifyResultStore,
    	              fieldLabel: '资产初分结果',
    	              valueField : 'code',
    	              displayField : 'name',
    	              allowBlank : true,
    	              typeAhead : true,
    	              readOnly: true,
    	              width: 150,
    	              mode:'local',  
    	              hiddenName:'firstResult',
    	              editable: false,
    	              triggerAction : 'all',
    	              selectOnFocus : true
    	}]},

{ layout : "form",
    items : [{
xtype: 'textarea',
id: 'classifyDesc', 
name: 'classifyDesc', 
fieldLabel: '资产初分设定原因', 
allowBlank: true,  
readOnly:true,
maxLength:200,
height:100,
anchor: '97%' ,
}]},

{ layout : "form",
    items : [{
xtype: 'combo', 
id : "confirmResult",
name: 'confirmResult', 
       store :classifyResultStore,
       fieldLabel: '调整后分类结果',
       valueField : 'code',
       displayField : 'name',
       emptyText : '请选择分类结果...',
       allowBlank : false,
       blankText:'请选择分类结果',
       typeAhead : true,
       width: 150,
       mode:'remote',  
       hiddenName:'confirmResult',
       editable: false,
       triggerAction : 'all',
       selectOnFocus : true
}]},
{ layout : "form",
           items : [{
xtype: 'textarea',
id: 'confirmDesc', 
name: 'confirmDesc', 
fieldLabel: '调整初分结果原因说明', 
emptyText: '必填', 
blankText: '调整原因说明不能为空', 
allowBlank: false,  
maxLength:100,
maxLengthText:'最多只允许输入200个汉字',
height:100,
anchor: '97%' 
}]},
{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "serialNo",
              fieldLabel: '分类流水号',
              allowBlank: true
}]}
]}]});

//新增窗口 
addWin = new Ext.Window({ 
id: 'addWin', 
title: '调整初分结果', 
width: 500, 
height: 400, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin').hide(); } }

] 
});

function adjustFirstResult(){
		if(addForm.form.isValid()){
		addForm.form.doAction('submit', { 
			url : 'obligation/adjustFirstResult.do', 
			method : 'post', 
			success : function() { 
				grid.store.reload();
				Ext.Msg.alert('提示', '调整初分结果成功！', function(btn){addWin.hide();}); 
			}, 
			failure : function() { 
				Ext.Msg.alert('提示', '调整失败！'); 
			} 
		});  
	}
}

//添加按钮单击事件 
function btnAddClick() {
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的内容！"); 
	}
	else{
		var selectedId = grid.getSelectionModel().getSelections()[0].data.serialNo;
		
		Ext.getCmp('addForm').form.reset();
		Ext.getCmp("serialNo").setValue(selectedId);
		
		//表单加载数据
		Ext.getCmp('addForm').form.load({  
	    waitMsg : '正在加载数据请稍后',
	    waitTitle : '提示',
	    url : 'obligation/getClassifyRecord.do', 
	    params:{serialNo: selectedId},
	    method:'post',
	    success:function(form,action){
	    	
	    },  
	    failure:function(form,action){
	     Ext.Msg.alert('提示','数据加载失败');  
	    },
		});
	    
	    
		Ext.getCmp("addWin").buttons[0].handler = adjustFirstResult; 
		Ext.getCmp("addWin").show(); 
	}
}; 



//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

function classifyConfirm(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的内容！"); 
	}
	else{
		var selectedId = grid.getSelectionModel().getSelections()[0].data.serialNo;
		var firstResult = grid.getSelectionModel().getSelections()[0].data.firstResult;
		
		Ext.MessageBox.confirm('提示', '您确定认定此分类结果吗？', 
			confirm = function(btn){
				if (btn == 'yes') {
		
			if(firstResult == '次级' || firstResult == '可疑' || firstResult == '损失'){
				Ext.MessageBox.confirm('提示', '当前操作可能会影响客户的违约标志，<br/>请关注是否需要调整该客户的违约标志!', 
				confirm = function(btn){
					if (btn == 'yes') {
					Ext.Ajax.request({
						url:'obligation/classifyConfirm.do',
						params : { serialNo: selectedId},
						method:'POST',
				  		success:function(){
				  			window.location.reload(); 
				  			//window.location.href="view/obligation/ClassifyRecordList_ToConfirm.jsp";
				  			//self.opener.location.reload(); 
						 	// grid.store.reload();
						 	 
					  },failure:function(){
					    Ext.MessageBox.alert("请求异常！");
					  }
					});
						return true;
					}else{ 
						return false;
					}
				}
				);
			}
			else{
				Ext.Ajax.request({
					url:'obligation/classifyConfirm.do',
					params : { serialNo: selectedId},
					method:'POST',
			  		success:function(){
			  			window.location.reload(); 
					 	// grid.store.reload();
				  },failure:function(){
				    Ext.MessageBox.alert("请求异常！");
				  }
				});
			}
				return true;
			}else{
				return false;
			}
		});
		

	}
}


function backToFirst(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的内容！"); 
	}
	else{
		var selectedId = grid.getSelectionModel().getSelections()[0].data.serialNo;

Ext.MessageBox.confirm('提示', '确定将结果退回初分人吗？', 
			confirm = function(btn){
	if (btn == 'yes') {
		Ext.Ajax.request({
		url:'obligation/backToFRUser.do',
		params : { serialNo: selectedId},
		method:'POST',
  		success:function(response){
		 	 grid.store.reload();
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); }
		});
}
}

//grid.addListener('rowdblclick',checkDetail);
}); 